<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class CoreZoneAreaBase extends BaseItemBase {
	
	//constructor
	function CoreZoneAreaBase() {
		parent::BaseItemBase();
		
		$this->table = 'pp_core_zones_areas';
		$this->prefix = 'zones_';
		$this->_table_owner_usable = false;
		$this->item_fields = array('id', 'name', 'code');
		$this->sort_fields = array('=countries_id+', 'code+', 'name+');
		$this->filter_fields = array('=countries_id:=');
		$this->filter_map = array('country' => '=countries_id:=');
	}
	
	//methods
	function getCountriesFormat() {
		$countries = $this->getItems('', 0, false);
		for($i=0, $n=sizeof($countries); $i<$n; $i++) {
			$countries_format[$countries[$i]['id']] = $countries[$i]['name'];
		}
		
		return $countries_format;
	}
	
	function getFormat($filter = '', $check = 0, $limit = true) {
		return $this->getItemsFormat($filter, $check, $limit, array('id', 'name'));
	}
	
	function getFormatList($filter = '', $check = 0, $limit = true) {
		$format = $this->format;
		$format_check = $this->format_check;
		$this->format = true;
		$this->format_check = false;
		
		$items = $this->getItems($filter, $check, $limit);
		$this->format = $format;
		$this->format_check = $format_check;
		
		return $items;
	}
	
	//
	function getCountriesItems($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and z.zones_status = 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and z.zones_name like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['parent']) && $filter['parent'] > 0 ) {
				$query_str .= " and z.zones_parent = '" . (int)$filter['parent'] . "' ";
			}
			if( isset($filter['country']) && $filter['country'] > 0 ) {
				$query_str .= " and z.countries_id = '" . (int)$filter['country'] . "' ";
			}
			if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and z.zones_id in (" . $this->_implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and z.zones_id = '" . (int)$filter['ids'] . "' ";
				}
			}
		}
		
		$query = "select z.*, c.* from pp_core_zones_areas z left join pp_core_countries c on z.countries_id = c.countries_id where 1 " . $query_str 
				. " order by z.countries_id asc, z.zones_code asc, z.zones_name asc " 
				. $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getCountriesItemsFormat($filter = '', $check = 0, $limit = true) {
		$format = array();
		$items = $this->getCountriesItems($filter, $check, $limit);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$format[$items[$i]['countries_id'] . ':' . $items[$i]['zones_id']] = $items[$i]['countries_name'] . '/' . $items[$i]['zones_name'];
		}
		
		return $format;
	}
}
//
?>